//#pragma once  Only use header guards in .h files!  They are not necessary anywhere else and can lead to bugs.
#include "Message Dispatcher.h"
#include "Entity Manager.h"
#include "ExecutionLog.h"

/*	-------------------------Discharge------------------------------
	This method merely calls the HandleMessage method of the receiver,
	which returns false if something goes wrong. Thus, an error report
	is printed if something goes wrong. */

void MessageDispatcher::Discharge(BaseGameEntity* pReceiver, const Message& message)
{
	if(!pReceiver->HandleMessage(message))
		/*Print an error message */;
}

string GetTypeString(MessageType type)
{
	switch(type)
	{
	case MoveToLocation:
		return "MoveToLocation";
		break;
	case ArriveAtLocation:
		return "ArriveAtLocation";
		break;
	default:
		return "Default";
		break;
	}

	return "Default"; // Just incase
}

void MessageDispatcher::DispatchMessage(int sender, int receiver, MessageType type, void* ExtraInfo)
{
	if(sender >= 0) // Sometimes the sender is marked as the player or another noncharacter
		BaseGameEntity* pSender = EntityMgr->GetEntityFromID(sender);
	BaseGameEntity* pReceiver = EntityMgr->GetEntityFromID(receiver);

	char buffer[100];
	string m_sender = itoa(sender, buffer, 10);
	string m_receiver = itoa(receiver, buffer, 10);
	string m_messageType = GetTypeString(type);

	//Log->AddMessage("Sent Message: " + m_messageType + " from " + m_sender + " to " + m_receiver);
	
	if (pReceiver == NULL)
	{
		cout << "\nWarning! No Receiver with ID of " << receiver << " found";
		return;
	}

	Message message(sender, receiver, type, ExtraInfo);

	Discharge(pReceiver, message);

}